package j05fandian;

class Chushi extends Thread{
    public void run() {
        while (true){
            if(J1.num==1){
                //有馒头
                System.out.println("厨师：还有馒头，开始休息...");
                synchronized (J1.class){
                    try {
                        J1.class.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }else {
                //没有馒头
                System.out.println("厨师：没有馒头，开始做馒头，等我1s...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                J1.num=1;
                System.out.println("厨师：1s结束，馒头做好了，通知食客吃馒头");

                synchronized (J1.class){
                    J1.class.notify();
                }
            }
        }
    }
}

class Shike extends Thread{
    public void run() {
        while (true){
            if(J1.num==0){
                System.out.println("食客食客：没有馒头，通知厨师做馒头");
                synchronized (J1.class){
                    J1.class.notify();
                    System.out.println("食客食客：开始休息...");
                    try {
                        J1.class.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }else {
                System.out.println("食客食客：有馒头，开始吃，等我1s...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                J1.num = 0;
                System.out.println("食客食客：馒头吃完了");
            }
        }
    }
}

public class J1 {
    static int num = 0;

    public static void main(String[] args) {
        Chushi chushi = new Chushi();
        chushi.start();

        Shike shike = new Shike();
        shike.start();
    }
}
